package com.dino.manager.entities;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.dino.manager.AbstractPage;
import com.test.dao.SearchCriteria;
import com.test.dao.SearchResultSet;
import com.test.mapper.enitites.EntityModel;
import com.test.mapper.services.EntityService;

public class EntitySearch extends AbstractPage {

	/**
	 * Generated serial version UID.
	 */
	private static final long serialVersionUID = 5894267201878896760L;

	@SpringBean
	private EntityService entityService;

	public EntitySearch() {
		this(new SearchCriteria(0, 0));
	}

	public EntitySearch(final SearchCriteria criteria) {
		add(new SearchForm("searchForm"));
		final SearchResultSet<EntityModel> clients = entityService.searchForEntities(criteria);
		final List<EntityModel> entityList = new ArrayList<EntityModel>(clients.getResultList());
		final ListView<EntityModel> rows = new ListView<EntityModel>("resultSet", entityList) {

			/**
			 * Generated serial version UID.
			 */
			private static final long serialVersionUID = 5816898920556209721L;

			@Override
			protected void populateItem(ListItem<EntityModel> item) {
				final EntityModel entity = item.getModelObject();
				item.add(new Label("clientId", entity.getClientId() + ""));
				final Link<EditEntity> editEntityLink = new Link<EditEntity>("editEntityLink") {

					/**
					 * Generated serial version UID.
					 */
					private static final long serialVersionUID = 8952326329426864574L;

					@Override
					public void onClick() {
						setResponsePage(new EditEntity(entity));
					}
				};
				item.add(editEntityLink);
				editEntityLink.add(new Label("entityName", entity.getEntityName()));
				item.add(new Label("tableName", entity.getTableName()));
				item.add(new Label("pkName", entity.getPrimaryKey().getPropertyName()));
				item.add(new Label("pkCol", entity.getPrimaryKey().getColumnName()));
			}
		};
		add(rows);
	}

	private class SearchForm extends Form<Object> {

		/**
		 * Generated serial version UID.
		 */
		private static final long serialVersionUID = 2182877376038127616L;

		private final TextField<String> clientIdField = new TextField<String>("clientId", new Model<String>());

		private final TextField<String> entityNameField = new TextField<String>("entityName", new Model<String>());

		private final TextField<String> tableNameField = new TextField<String>("tableName", new Model<String>());

		public SearchForm(final String id) {
			super(id);
			add(clientIdField);
			add(entityNameField);
			add(tableNameField);
			add(new SubmitLink("search") {

				/**
				 * Generated serial version UID.
				 */
				private static final long serialVersionUID = 2946080983274882079L;

				@Override
				public void onSubmit() {
					final SearchCriteria criteria = new SearchCriteria();
					final String searchedClientId = clientIdField.getModelObject();
					if (StringUtils.isNotEmpty(searchedClientId)) {
						criteria.addPropertyCriteria("clientId", Arrays.asList(searchedClientId.split(" ")));
					}
					final String searchedEntityName = entityNameField.getModelObject();
					if (StringUtils.isNotEmpty(searchedEntityName)) {
						criteria.addPropertyCriteria("entityName", Arrays.asList(searchedEntityName.split(" ")));
					}
					final String searchedTableName = tableNameField.getModelObject();
					if (StringUtils.isNotEmpty(searchedTableName)) {
						criteria.addPropertyCriteria("tableName", Arrays.asList(searchedTableName.split(" ")));
					}
					criteria.setItemsPerPage(0);
					setResponsePage(new EntitySearch(criteria));
				}
			});
		}
		
	}
}
